package com.food.dao;

import hibernate.util.HibernateUtil;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.classic.Session;

import com.food.dao.in.ProductDAO_hiber;
import com.food.model.Product;

public class Product_hiber implements ProductDAO_hiber {

	@Override
	public Product findByPrimaryKey(Integer pid) {
		Product bean = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			bean = (Product) session.get(Product.class, pid);
			session.getTransaction().commit();
		} catch (RuntimeException ex) {
			session.getTransaction().rollback();
			throw ex;
		}
		return bean;
	}

	@Override
	public void insert(Product bean) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			session.saveOrUpdate(bean);
			session.getTransaction().commit();
		} catch (RuntimeException ex) {
			session.getTransaction().rollback();
			throw ex;
		}
	}

	@Override
	public void delete(Integer pid) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			Product bean = (Product) session.get(Product.class, pid);
			session.delete(bean);
			session.getTransaction().commit();
		} catch (RuntimeException ex) {
			session.getTransaction().rollback();
			throw ex;
		}
	}

	@Override
	public void update(Product bean) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			session.update(bean);
			session.getTransaction().commit();
		} catch (RuntimeException ex) {
			session.getTransaction().rollback();
			throw ex;
		}

	}

	@Override
	public long getAllCount() {
		long count = 0;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			Query query = session.createQuery("select COUNT(*) from Product");
			count = (Long) query.list().get(0);
			session.getTransaction().commit();
		} catch (RuntimeException ex) {
			session.getTransaction().rollback();
			throw ex;
		}
		return count;
	}

	@Override
	public List<Product> getAll(Integer offset, Integer length) {
		List<Product> list = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();

			// 基礎語法(請注意空白)
			String hql1 = "from Product";

			String hql = hql1;

			Query query = session.createQuery(hql);

			query.setFirstResult(offset); // 設定起始
			query.setMaxResults(length); // 每頁顯示
			list = query.list();

			session.getTransaction().commit();
		} catch (RuntimeException ex) {
			session.getTransaction().rollback();
			throw ex;
		}
		return list;
	}

	@Override
	public List<Product> searchCust(Integer offset, Integer length, String searchField, String searchString) {
		List<Product> list = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();

			String hql1 = "from Product where ";

			// 搜尋條件(多選擇,自行考慮實作,注意搜尋字串要加 ' ' 符號 ex: 'searchString')
			String hql2 = searchField + "=" + "\'" + searchString + "\'";

			String hql = hql1 + hql2;

			Query query = session.createQuery(hql);

			query.setFirstResult(offset); // 設定起始
			query.setMaxResults(length); // 每頁顯示
			list = query.list();

			session.getTransaction().commit();
		} catch (RuntimeException ex) {
			session.getTransaction().rollback();
			throw ex;
		}
		return list;
	}

	@Override
	public long searchCustCount(String searchField, String searchString) {
		long count = 0;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();

			String hql1 = "select COUNT(*) from Product where ";

			// 搜尋條件(多選擇,自行考慮實作,注意搜尋字串要加 ' ' 符號 ex: 'searchString')
			String hql2 = searchField + "=" + "\'" + searchString + "\'";

			String hql = hql1 + hql2;

			Query query = session.createQuery(hql);
			count = (Long) query.list().get(0);
			session.getTransaction().commit();
		} catch (RuntimeException ex) {
			session.getTransaction().rollback();
			throw ex;
		}
		return count;
	}

	@Override
	public List<Product> getAll() {
		List<Product> list = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			Query query = session.createQuery("from Product");
			list = query.list();
			session.getTransaction().commit();
		} catch (RuntimeException ex) {
			session.getTransaction().rollback();
			throw ex;
		}
		return list;
	}

	public static void main(String[] args) {

		Product_hiber dao = new Product_hiber();
		System.out.println(dao.findByPrimaryKey(14).toString());
		System.out.println(dao.getAllCount());

		List<Product> xx = dao.getAll(0, 4);
		for (Product bean : xx) {
			System.out.println(bean);
		}

		List<Product> yy = dao.searchCust(0, 4, "pcountry", "台灣");

		for (Product bean : yy) {
			System.out.println(bean);
		}

		System.out.println(dao.searchCustCount("pcountry", "台灣"));
		
		List<Product> zz = dao.getAll();

		for (Product bean : zz) {
			System.out.println(bean);
		}
		

	}

}
